package com.liu.msmService.service.impl;

import com.liu.msmService.service.MsmService;
import com.liu.msmService.utils.HttpUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.http.HttpResponse;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * **************
 * 项目名称: testgulili <br/>
 * 文件名称:  <br/>
 * 文件描述: 这里添加您的类文件描述，说明当前文件要包含的功能。 <br/>
 * 文件创建：刘世锦 <br/>
 * 创建时间: 2022/1/30 <br/>
 *
 * @version v1.0 <br/>
 * @update [序号][日期YYYY-MM-DD][更改人姓名][变更描述]<br/>
 * *************
 */
@Service
public class MsmServiceImpl implements MsmService {
    public static String AppCode = "1a3d3ec1e6f043e7b4ca3fa5a28222f6";

    @Override
    public boolean send(String code, String phone) {
        if (ObjectUtils.isEmpty(code)) {
            System.out.println("00000000000000 验 证 码 为 空 0000000000000:>" + code);
            return  false;
        } else {

            String host = "https://dfsns.market.alicloudapi.com";
            String path = "/data/send_sms";
            String method = "POST";
            String appcode = AppCode;
            Map<String, String> headers = new HashMap<String, String>();
            //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
            headers.put("Authorization", "APPCODE " + appcode);
            //根据API的要求，定义相对应的Content-Type
            headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            Map<String, String> querys = new HashMap<String, String>();
            Map<String, String> bodys = new HashMap<String, String>();
//        传入验证码
            bodys.put("content", "code:" + code);
            System.out.println("========== 验 证 码 =============： " + code);
            bodys.put("phone_number", phone);
            bodys.put("template_id", "TPL_0000");

            try {
                HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
                System.out.println(response.toString());
                //获取response的body
                //System.out.println(EntityUtils.toString(response.getEntity()));
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;

        }

    }
}
